package handler

import (
	"context"
	"examination-management/srv/cmd/config"
	"examination-management/srv/cmd/model"
	"examination-management/srv/pkg"
	pb "examination-management/srv/proto"
	"time"
)

func (s *Server) CreateUser(_ context.Context, in *pb.CreateUserReq) (*pb.CreateUserResp, error) {
	var u model.User
	config.DB.Debug().Where("mobile = ?", in.Mobile).Limit(1).Find(&u)
	if u.Id != 0 {
		return &pb.CreateUserResp{
			Message: "已注册",
			Code:    10001,
		}, nil
	}
	user := model.User{
		Mobile:   in.Mobile,
		Password: pkg.Md5(in.Password),
	}
	err := config.DB.Debug().Create(&user).Error
	if err != nil {
		return &pb.CreateUserResp{
			Message: "注册失败",
			Code:    10002,
		}, nil
	}
	return &pb.CreateUserResp{
		Message: "注册成功",
		Code:    200,
	}, nil
}
func (s *Server) LoginUser(_ context.Context, in *pb.LoginUserReq) (*pb.LoginUserResp, error) {
	var u model.User
	config.DB.Debug().Where("mobile = ?", in.Mobile).Limit(1).Find(&u)
	if u.Id == 0 {
		return &pb.LoginUserResp{
			Message: "未注册",
			Code:    10001,
		}, nil
	}
	if u.Password != pkg.Md5(in.Password) {
		return &pb.LoginUserResp{
			Message: "密码错误",
			Code:    10002,
		}, nil
	}
	return &pb.LoginUserResp{
		Message: "登录成功",
		Code:    200,
		Id:      u.Id,
	}, nil
}
func (s *Server) UpdateUser(_ context.Context, in *pb.UpdateUserReq) (*pb.UpdateUserResp, error) {
	var u model.User
	config.DB.Debug().Where("id = ?", in.Id).Limit(1).Find(&u)
	if u.Id == 0 {
		return &pb.UpdateUserResp{
			Message: "未登录",
			Code:    10001,
		}, nil
	}
	UIdCard := time.Now().Format("20060102150405")
	user := model.User{
		Id:     in.Id,
		Name:   in.Name,
		IdCard: UIdCard,
		UType:  in.UType,
	}
	err := config.DB.Debug().Updates(&user).Error
	if err != nil {
		return &pb.UpdateUserResp{
			Message: "更新失败",
			Code:    10002,
		}, nil
	}
	return &pb.UpdateUserResp{
		Message: "更新成功",
		Code:    200,
	}, nil
}
func (s *Server) QueryUserInfo(_ context.Context, in *pb.QueryUserInfoReq) (*pb.QueryUserInfoResp, error) {
	var u model.User
	config.DB.Debug().Where("id = ?", in.Id).Limit(1).Find(&u)
	if u.Id == 0 {
		return &pb.QueryUserInfoResp{
			Message: "未登录",
			Code:    10001,
		}, nil
	}
	var info []model.UserInfo
	err := config.DB.Debug().Find(&info).Error
	if err != nil {
		return &pb.QueryUserInfoResp{
			Message: "查询失败",
			Code:    500,
			List:    nil,
		}, nil
	}
	var query []*pb.QueryUserInfo
	for _, userInfo := range info {
		if userInfo.Type == "已结束" {
			query = append(query, &pb.QueryUserInfo{
				TestQuestion:   userInfo.TestQuestion,
				TestPaperTypes: userInfo.TestPaperTypes,
				Type:           userInfo.Type,
				Fraction:       userInfo.Fraction,
			})
		}
	}
	return &pb.QueryUserInfoResp{
		Message: "查询成功",
		Code:    200,
		List:    query,
	}, nil
}
func (s *Server) QueryUserInfoTime(_ context.Context, in *pb.QueryUserInfoTimeReq) (*pb.QueryUserInfoTimeResp, error) {
	var u model.User
	config.DB.Debug().Where("id = ?", in.Id).Limit(1).Find(&u)
	if u.Id == 0 {
		return &pb.QueryUserInfoTimeResp{
			Message: "未登录",
			Code:    10001,
		}, nil
	}
	var info model.UserInfo
	config.DB.Debug().Where("id = ?", in.Id).Limit(1).Find(&info)
	if info.Type != "进行中" {
		return nil, nil
	}
	return &pb.QueryUserInfoTimeResp{
		Message: "当前已不能考试",
		Code:    200,
	}, nil
}
